/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.client.LmcSession;
/*     */ import com.zimbra.cs.client.soap.LmcGetMsgRequest;
/*     */ import com.zimbra.cs.client.soap.LmcMsgActionRequest;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.Folder.FolderOptions;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.Tag;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import junit.framework.Assert;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestConcurrency
/*     */   extends TestCase
/*     */ {
/*     */   public static final String TAG_PREFIX = "TestConcurrency";
/*     */   public static final String FOLDER_NAME = "TestConcurrency";
/*     */   Account mAccount;
/*     */   Mailbox mMbox;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  51 */     this.mAccount = TestUtil.getAccount("user1");
/*  52 */     this.mMbox = MailboxManager.getInstance().getMailboxByAccount(this.mAccount);
/*     */     
/*     */ 
/*  55 */     cleanUp();
/*     */   }
/*     */   
/*     */   public void testRead() throws Exception
/*     */   {
/*  60 */     int numThreads = 5;
/*  61 */     Thread[] threads = new Thread[numThreads];
/*     */     
/*  63 */     for (int i = 0; i < numThreads; i++) {
/*  64 */       threads[i] = new Thread(new ReadMessagesThread(5), "ReadMessagesThread-" + i);
/*     */     }
/*     */     
/*  67 */     runThreads(threads);
/*     */   }
/*     */   
/*     */   public void testTag() throws Exception
/*     */   {
/*  72 */     int numThreads = 5;
/*  73 */     Thread[] threads = new Thread[numThreads];
/*     */     
/*  75 */     for (int i = 0; i < numThreads; i++) {
/*  76 */       Tag tag = this.mMbox.createTag(null, "TestConcurrency" + i + 1, (byte)0);
/*  77 */       threads[i] = new Thread(new TagMessagesThread(tag, 5), "TagMessagesThread-" + i);
/*     */     }
/*     */     
/*  80 */     runThreads(threads);
/*     */   }
/*     */   
/*     */   public void testReadAndTag() throws Exception
/*     */   {
/*  85 */     int numThreads = 6;
/*  86 */     Thread[] threads = new Thread[numThreads];
/*  87 */     int tagNum = 1;
/*     */     
/*  89 */     for (int i = 0; i < numThreads; i += 2) {
/*  90 */       Tag tag = this.mMbox.createTag(null, "TestConcurrency" + tagNum, (byte)0);
/*  91 */       threads[i] = new Thread(new ReadMessagesThread(5), "ReadMessagesThread-" + i);
/*  92 */       threads[(i + 1)] = new Thread(new TagMessagesThread(tag, 5), "TagMessagesThread-" + i);
/*  93 */       tagNum++;
/*     */     }
/*     */     
/*  96 */     runThreads(threads);
/*     */   }
/*     */   
/*     */   public void testReadAndMove() throws Exception {
/* 100 */     int numThreads = 5;
/* 101 */     Thread[] threads = new Thread[numThreads];
/*     */     
/*     */ 
/* 104 */     Folder folder = this.mMbox.createFolder(null, "TestConcurrency", 1, new Folder.FolderOptions());
/* 105 */     threads[0] = new Thread(new MoveMessagesThread(folder, 5));
/*     */     
/*     */ 
/* 108 */     for (int i = 1; i < numThreads; i++) {
/* 109 */       threads[i] = new Thread(new ReadMessagesThread(5));
/*     */     }
/*     */     
/* 112 */     runThreads(threads);
/*     */   }
/*     */   
/*     */   private void runThreads(Thread[] threads)
/*     */     throws Exception
/*     */   {
/* 118 */     for (int i = 0; i < threads.length; i++) {
/* 119 */       threads[i].start();
/*     */     }
/*     */     for (;;)
/*     */     {
/* 123 */       boolean allDone = true;
/* 124 */       for (int i = 0; i < threads.length; i++) {
/* 125 */         if (threads[i].isAlive()) {
/* 126 */           allDone = false;
/* 127 */           break;
/*     */         }
/*     */       }
/*     */       
/* 131 */       if (allDone) {
/* 132 */         return;
/*     */       }
/* 134 */       Thread.sleep(50L);
/*     */     }
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 140 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 145 */     List<Tag> tagList = this.mMbox.getTagList(null);
/* 146 */     if (tagList != null) {
/* 147 */       Iterator<Tag> i = tagList.iterator();
/* 148 */       while (i.hasNext()) {
/* 149 */         Tag tag = (Tag)i.next();
/* 150 */         if (tag.getName().startsWith("TestConcurrency")) {
/* 151 */           this.mMbox.delete(null, tag.getId(), tag.getType());
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 157 */     Folder folder = TestUtil.getFolderByPath(this.mMbox, "TestConcurrency");
/* 158 */     if (folder != null) {
/* 159 */       List<MailItem> ids = this.mMbox.getItemList(null, MailItem.Type.MESSAGE, folder.getId());
/* 160 */       Iterator<MailItem> i = ids.iterator();
/* 161 */       while (i.hasNext()) {
/* 162 */         Message message = (Message)i.next();
/* 163 */         this.mMbox.move(null, message.getId(), MailItem.Type.MESSAGE, 2);
/*     */       }
/* 165 */       this.mMbox.delete(null, folder.getId(), folder.getType());
/*     */     }
/*     */   }
/*     */   
/*     */   private class ReadMessagesThread implements Runnable
/*     */   {
/*     */     int mNumToRead;
/*     */     
/*     */     ReadMessagesThread(int numToRead) {
/* 174 */       this.mNumToRead = numToRead;
/*     */     }
/*     */     
/*     */     public void run()
/*     */     {
/* 179 */       ZimbraLog.test.debug("Starting ReadMessagesThread");
/* 180 */       int numRead = 0;
/*     */       try
/*     */       {
/* 183 */         session = TestUtil.getSoapSession("user1");
/*     */         
/*     */ 
/* 186 */         List<Integer> ids = TestUtil.search(TestConcurrency.this.mMbox, "in:inbox roland", MailItem.Type.MESSAGE);
/* 187 */         Assert.assertTrue("Search returned " + ids.size() + " messages.  Expected at least " + this.mNumToRead, ids.size() >= this.mNumToRead);
/*     */         
/*     */ 
/* 190 */         for (Integer id : ids) {
/* 191 */           LmcGetMsgRequest req = new LmcGetMsgRequest();
/* 192 */           req.setMsgToGet(id.toString());
/* 193 */           req.setSession(session);
/* 194 */           req.invoke(TestUtil.getSoapUrl());
/* 195 */           numRead++;
/* 196 */           if (numRead == this.mNumToRead)
/*     */             break;
/*     */         }
/*     */       } catch (Exception e) {
/*     */         LmcSession session;
/* 201 */         ZimbraLog.test.error("Error in ReadMessagesThread", e);
/*     */       }
/*     */       
/* 204 */       ZimbraLog.test.debug("ReadMessagesThread read " + numRead + " messages");
/*     */     }
/*     */   }
/*     */   
/*     */   private class TagMessagesThread implements Runnable
/*     */   {
/*     */     Tag mTag;
/*     */     int mNumToTag;
/*     */     
/*     */     TagMessagesThread(Tag tag, int numToTag) {
/* 214 */       this.mTag = tag;
/* 215 */       this.mNumToTag = numToTag;
/*     */     }
/*     */     
/*     */     public void run()
/*     */     {
/* 220 */       ZimbraLog.test.debug("Starting TagMessagesThread");
/* 221 */       int numTagged = 0;
/*     */       try
/*     */       {
/* 224 */         session = TestUtil.getSoapSession("user1");
/*     */         
/*     */ 
/* 227 */         List<Integer> ids = TestUtil.search(TestConcurrency.this.mMbox, "in:inbox", MailItem.Type.MESSAGE);
/*     */         
/* 229 */         for (Integer id : ids) {
/* 230 */           LmcMsgActionRequest req = new LmcMsgActionRequest();
/* 231 */           req.setOp("tag");
/* 232 */           req.setMsgList(id.toString());
/* 233 */           req.setTag(Integer.toString(this.mTag.getId()));
/* 234 */           req.setSession(session);
/* 235 */           req.invoke(TestUtil.getSoapUrl());
/* 236 */           numTagged++;
/* 237 */           if (numTagged == this.mNumToTag)
/*     */             break;
/*     */         }
/*     */       } catch (Exception e) {
/*     */         LmcSession session;
/* 242 */         ZimbraLog.test.error("Error in TagMessagesThread", e);
/*     */       }
/*     */       
/* 245 */       ZimbraLog.test.debug("TagMessagesThread tagged " + numTagged + " messages");
/*     */     }
/*     */   }
/*     */   
/*     */   private class MoveMessagesThread implements Runnable
/*     */   {
/*     */     Folder mFolder;
/*     */     int mNumToMove;
/*     */     
/*     */     MoveMessagesThread(Folder folder, int numToMove) {
/* 255 */       this.mFolder = folder;
/* 256 */       this.mNumToMove = numToMove;
/*     */     }
/*     */     
/*     */     public void run()
/*     */     {
/* 261 */       ZimbraLog.test.debug("Starting MoveMessagesThread");
/* 262 */       int numMoved = 0;
/*     */       try
/*     */       {
/* 265 */         session = TestUtil.getSoapSession("user1");
/* 266 */         List<Integer> ids = TestUtil.search(TestConcurrency.this.mMbox, "in:inbox", MailItem.Type.MESSAGE);
/*     */         
/* 268 */         for (Integer id : ids) {
/* 269 */           LmcMsgActionRequest req = new LmcMsgActionRequest();
/* 270 */           req.setOp("move");
/* 271 */           req.setMsgList(id.toString());
/* 272 */           req.setFolder(Integer.toString(this.mFolder.getId()));
/* 273 */           req.setSession(session);
/* 274 */           req.invoke(TestUtil.getSoapUrl());
/* 275 */           numMoved++;
/* 276 */           if (numMoved == this.mNumToMove)
/*     */             break;
/*     */         }
/*     */       } catch (Exception e) {
/*     */         LmcSession session;
/* 281 */         ZimbraLog.test.error("Error in MoveMessagesThread", e);
/*     */       }
/*     */       
/* 284 */       ZimbraLog.test.debug("MoveMessagesThread moved " + numMoved + " messages");
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestConcurrency.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */